package com.example.wheresmystuff2;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * This class is a helper that implements SQLiteOpenHelper
 * to make the database
 * @author Team 26
 */
public class DatabaseHelper extends SQLiteOpenHelper{
	
	private static final String DB_NAME = "Database"; //database's name
	private static final int DB_VERSION = 1; // the database version number
	
	//Variables for user database table
	public static final String USER_TABLE_NAME = "usernameTable";
	public static final String COL_USER_ID = "_id";
	public static final String COL_USERNAME = "username";
	public static final String COL_PASSWORD = "password";
	public static final String COL_EMAIL = "email";
	public static final String COL_COUNT = "counter";
	public static final String COL_LOCKED = "locked";
	public static final String COL_ADMIN = "admin";
	
	//Variables for item database table
	public static final String ITEM_TABLE_NAME = "itemtable";
	public static final String COL_ITEM_ID = "_id";
	public static final String COL_ITEM = "item"; 
	public static final String COL_DESCRIPTION = "description"; 
	public static final String COL_LOCATION = "location"; 
	public static final String COL_STATUS = "status"; 
	public static final String COL_CATEGORY = "category"; 
	public static final String COL_TYPE = "type"; 
	public static final String COL_DATE = "date"; 
	public static final String COL_REWARD = "reward"; 
	
	//Table Database Statements so it isn't as cluttered 
	final String USER_DATABASE_STATEMENT = "create table " +
			USER_TABLE_NAME + " (" +
			COL_USER_ID + " integer primary key autoincrement, " +
			COL_USERNAME + " text not null, " +
			COL_PASSWORD + " text not null, " +
			COL_EMAIL + " text not null, " +
			COL_COUNT + " integer not null, " +
			COL_LOCKED + " integer not null, " +
			COL_ADMIN + " integer not null" +
			");";
		
	final String ITEM_DATABASE_STATEMENT = "create table " +
			ITEM_TABLE_NAME + " (" +
			COL_ITEM_ID + " integer primary key autoincrement, " +
			COL_ITEM + " text not null, " +
			COL_DESCRIPTION + " text not null, " +
			COL_LOCATION + " text not null, " +
			COL_STATUS + " text not null, " +
			COL_CATEGORY + " text not null, " +
			COL_TYPE + " text not null, " +
			COL_DATE + " text not null, " +
			COL_REWARD + " text not null" +
			");";
	
	/**
	 * This constructor for the DatabaseHelper
	 * @param context Takes in the method in which this will be called
	 */
	public DatabaseHelper(Context context){
		super(context, DB_NAME, null, DB_VERSION);
	}
	
	/**
	 * This method creates a database if there isn't one, with the parameters given.
	 */
	@Override
	public void onCreate(SQLiteDatabase db){
		Log.d(DatabaseHelper.class.getName(), "Ready to create database");
		
		db.execSQL(USER_DATABASE_STATEMENT);
		db.execSQL(ITEM_DATABASE_STATEMENT);
		
		//creates default admin
		ContentValues values = new ContentValues();
		values.put(DatabaseHelper.COL_USERNAME, "admin");	
		values.put(DatabaseHelper.COL_PASSWORD, "admin");
		values.put(DatabaseHelper.COL_EMAIL, "admin");
		values.put(DatabaseHelper.COL_COUNT, 3);
		values.put(DatabaseHelper.COL_LOCKED, 0);
		values.put(DatabaseHelper.COL_ADMIN, 1);
		
		db.insert(DatabaseHelper.USER_TABLE_NAME, null, values);
		
		//creates default user
		values.put(DatabaseHelper.COL_USERNAME, "poop");	
		values.put(DatabaseHelper.COL_PASSWORD, "poop");
		values.put(DatabaseHelper.COL_EMAIL, "poop");
		values.put(DatabaseHelper.COL_COUNT, 3);
		values.put(DatabaseHelper.COL_LOCKED, 0);
		values.put(DatabaseHelper.COL_ADMIN, 0);
		
		db.insert(DatabaseHelper.USER_TABLE_NAME, null, values);
		
		Log.d(DatabaseHelper.class.getName(),"Created Database");
	}
	/**
	 * This method upgrades our database with a new version. 
	 * Unused because our current is the only database version.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
	{
		Log.w(DatabaseHelper.class.getName(),
		        "Upgrading database from version " + oldVersion + " to "
		            + newVersion + ", which will destroy all old data");
		    db.execSQL("DROP TABLE IF EXISTS " + USER_TABLE_NAME);
		    db.execSQL("DROP TABLE IF EXISTS " + ITEM_TABLE_NAME);
		    onCreate(db);
	}
}

